/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.eurotrip.mobile.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import it.eurotrip.mobile.model.Poi;
import it.eurotrip.mobile.model.PoiType;
import java.io.File;
import java.io.InputStream;
import java.sql.SQLException;

/**
 *
 * @author Dario
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "valencia.db";
    private static final int DATABASE_VERSION = 1;
    private Dao<Poi, Integer> poiDAO;
    private Dao<PoiType, Integer> poiTypeDAO;

    public DatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion, InputStream stream) {
        super(context, databaseName, factory, databaseVersion, stream);
    }

    public DatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion, File configFile) {
        super(context, databaseName, factory, databaseVersion, configFile);
    }

    public DatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion, int configFileId) {
        super(context, databaseName, factory, databaseVersion, configFileId);
    }

    public DatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion) {
        super(context, databaseName, factory, databaseVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase sqld, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, Poi.class);
            TableUtils.createTable(connectionSource, PoiType.class);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Unable to create datbases", e);
        }
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqld, ConnectionSource cs, int oldVer, int newVer) {
        try {
            TableUtils.dropTable(cs, Poi.class, true);
            TableUtils.dropTable(cs, PoiType.class, true);
            onCreate(sqld, cs);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Unable to upgrade database from version " + oldVer + " to new "+ newVer, e);
        }
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public Dao<Poi, Integer> getPoiDao() throws SQLException {
        if (poiDAO == null) {
            poiDAO = getDao(Poi.class);
        }
        return poiDAO;
    }

    public Dao<PoiType, Integer> getEventDao() throws SQLException {
        if (poiTypeDAO == null) {
            poiTypeDAO = getDao(PoiType.class);
        }
        return poiTypeDAO;
    }
}
